#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;

int getRamdom(int delta)
{
    return rand() % delta;
}

bool ramdomQuestion()
{
    cout << "请答题:" << endl;

retry:
    int x = getRamdom(9);
    int y = getRamdom(9);
    switch (getRamdom(4))
    {
    case 0: // +
    {
        printf("请回答 %d + %d = ", x, y);
        int result;
        cin >> result;
        if (x + y == result)
        {
            return true;
        }
        return false;
        break;
    }
    case 1: // -
    {
        //减法不能出现负值
        if (x - y < 0)
        {
            goto retry;
        }
        printf("请回答 %d - %d = ", x, y);
        int result;
        cin >> result;
        if (x - y == result)
        {
            return true;
        }
        return false;
        break;
    }
    case 2: // *
    {
        printf("请回答 %d * %d = ", x, y);
        int result;
        cin >> result;
        if (x * y == result)
        {
            return true;
        }
        return false;
        break;
    }
    case 3: // /
    {
        //除法不能出现除0
        if (y == 0)
        {
            goto retry;
        }
        printf("请回答 %d / %d = ", x, y);
        int result;
        cin >> result;
        if (x / y - result < 1e6)
        {
            return true;
        }
        return false;
        break;
    }
    }
    return false;
}

int setQuestion()
{
    bool ok = ramdomQuestion();
    if (ok)
    {
        cout << "结果正确, ";
    }
    else
    {
        cout << "结果错误, ";
    }

    return 0;
}

int main()
{
    cout << endl
         << "学号：3170608043，姓名：石英钰" << endl
         << endl
         << "Features: 随机出题" << endl
         << endl;

    srand((unsigned)time(NULL)); // 需要放在前面
    setQuestion();
    for (;;)
    {
        cout << "继续答题吗?(Y/N): ";
        char isc;
        cin >> isc;
        if (isc == 'Y' || isc == 'y')
        {
            setQuestion();
            continue;
        }
        else
        {
            cout << "Aborted!" << endl;
            break;
        }
    }
    return 0;
}